def contains_dup(nums, k):
    seen = {}
    for i in range(len(nums)):
        num = nums[i]
        if num in seen:
            j = seen.get(num)
            if i-j <= k:
                return True
        seen[num] = i
    return False

print(contains_dup([1,2,3,1,2,3], 2))
print(contains_dup([1,2,3,1], 3))